package com.yong.ssj.control.consumer.order;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yong.ssj.db.dto.cost.CostMethodDto;
import com.yong.ssj.service.order.IOrderService;

@Controller
@RequestMapping("/consumer/order")
public class OrderControllerph {
	@Resource(name="OrderServiceImpl")
	private IOrderService orderService;

	@RequestMapping("borrow")
	public String costList(ModelMap res){//申请借款页面
		List<Map<String, Object>> dto = orderService.queryCost();
		res.put("cost", dto);
		return "consumer/order/borrow";
	}
	
	@RequestMapping("borrow_do")
	public String borrowString(String apply_money,String month,HttpSession session,ModelMap res){//申请借款操作month为方案的id
		String customer_id = (String)session.getAttribute("customer_id");
		double money = 0;
		try {
			money = Double.parseDouble(apply_money);
		} catch (Exception e) {
			// TODO: handle exception
			res.put("tishi", "金额输入不正确！请重新输入！");
			return "consumer/order/borrow";
		}
		double rz_surplus_money = (Double)session.getAttribute("rz_surplus_money");
		if(rz_surplus_money<money){
			res.put("tishi", "可用额度不足！请重新输入！");
			return "consumer/order/borrow";
		}else if(money>0&&money%100==0){
			int yan = orderService.insertBorrow(money,month,Long.parseLong(customer_id) );//操作
		}else{
			res.put("tishi", "金额输入不正确！请重新输入！");
			return "consumer/order/borrow";
		}
		return "consumer/order/myself";
	}
	
	@RequestMapping("myself")
	public String myself(){//我的详情页面
		return "consumer/order/myself";
	}
	
	@RequestMapping("queryborrow")
	public String queryborrowList(HttpSession session,ModelMap res){//我的借款未完成页面
		String customer_id = (String)session.getAttribute("customer_id");
		List<Map<String, Object>> list = orderService.queryborrowList(Long.parseLong(customer_id));
		res.put("orderList", list);
		return "consumer/order/queryborrow";
	}
	
	@RequestMapping("queryborrowed")
	public String queryborrowedList(HttpSession session,ModelMap res){//我的借款已经完成页面
		String customer_id = (String)session.getAttribute("customer_id");
		List<Map<String, Object>> list = orderService.queryborrowedList(Long.parseLong(customer_id));
		res.put("orderList", list);
		return "consumer/order/queryborrowed";
	}
	
	@RequestMapping("queryhuan")
	public String queryhuanList(HttpSession session,ModelMap res){//我的还款页面
		String customer_id = (String)session.getAttribute("customer_id");
		List<Map<String, Object>> list = orderService.queryhuanList(Long.parseLong(customer_id));
		res.put("orderList", list);
		return "consumer/order/queryhuan";
	}
	@RequestMapping("tiqian/{order_id}")
	public String tiqianList(ModelMap res,@PathVariable String order_id){//提前还款页面
		List<Map<String, Object>> list = orderService.querytiqianList(Long.parseLong(order_id));
		Map<String, Object> map = list.get(0);
		double principal = (Double)map.get("principal");
		double cost_day_rate = (Double)map.get("cost_day_rate");
		Calendar now = Calendar.getInstance();
		int today = now.get(Calendar.DAY_OF_MONTH);
		now.set(Calendar.DATE-1, 1);
		now.roll(Calendar.DATE-1, -1);
		int maxDate = now.get(Calendar.DATE);//前一个月的天数
		int day = 0;
		if(today>10){
			day = today - 10 ;
		}
		if(today<10){
			day = maxDate + today - 10;
		}
		double money = day*cost_day_rate*principal; //利息
		res.put("orderList", list);
		res.put("money", money);
		return "consumer/order/tiqian";
	}
	@RequestMapping("tiqian_do/{order_id}")
	public String tiqiandoList(ModelMap res,@PathVariable String order_id){//提前还款
		List<Map<String, Object>> list = orderService.querytiqianList(Long.parseLong(order_id));
		Map<String, Object> map = list.get(0);
		double principal = (Double)map.get("principal");
		double cost_day_rate = (Double)map.get("cost_day_rate");
		Calendar now = Calendar.getInstance();
		int today = now.get(Calendar.DAY_OF_MONTH);
		now.set(Calendar.DATE-1, 1);
		now.roll(Calendar.DATE-1, -1);
		int maxDate = now.get(Calendar.DATE);//前一个月的天数
		int day = 0;
		if(today>10){
			day = today - 10 ;
		}
		if(today<10){
			day = maxDate + today - 10;
		}
		double money = day*cost_day_rate*principal+principal; //需要还款的金额
		int yan = orderService.querytiqianedList(Long.parseLong(order_id),money);
		return "consumer/order/queryhuan";
	}
	
	@RequestMapping("xiangqing/{order_id}")
	public String xiangqingList(ModelMap res,@PathVariable String order_id){//还款详情
		List<Map<String, Object>> list = orderService.querytiqianList(Long.parseLong(order_id));
		res.put("orderList", list);
		return "consumer/order/xiangqing";
	}
}
